﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Common {
    public class Settings {
        

        public Dictionary<string, string> InnerSettings;

        public Settings() {
            InnerSettings = new Dictionary<string, string>();
            string[] setArr = File.ReadAllLines(Funcs.GetAppPath() + "ebay.config");
            foreach (string line in setArr) {
                if (line.Trim() == "" || line.Trim().StartsWith("//")) continue;
                string[] keyVal = line.Split('=');
                if (keyVal[0].Trim() == "" || keyVal[1].Trim() == "") continue;
                InnerSettings.Add(keyVal[0].Trim(), keyVal[1].Trim());
            }

            if (InnerSettings["DevID"] == ""
                || InnerSettings["AppID"] == ""
                || InnerSettings["CertID"] == ""
                || InnerSettings["UserToken"] == ""
                || InnerSettings["ServerUrl"] == ""
                || InnerSettings["SiteID"] == "")
                throw new Exception();
        }

        public string DevID { get { return InnerSettings["DevID"]; } }
        public string AppID { get { return InnerSettings["AppID"]; } }
        public string CertID { get { return InnerSettings["CertID"]; } }
        public string UserToken { get { return InnerSettings["UserToken"]; } }
        public string ServerUrl { get { return InnerSettings["ServerUrl"]; } }
        public string SiteID { get { return InnerSettings["SiteID"]; } }

        public string ShoppingAPIUrl { get { return InnerSettings["ShoppingAPIUrl"]; } }
        public string FindingAPIUrl { get { return InnerSettings["FindingAPIUrl"]; } }
    }

    public class Funcs {
        public static string GetAppPath() {
            string path = System.Reflection.Assembly.GetExecutingAssembly().Location;

            path = path.Substring(0, path.LastIndexOf(@"\"));

            string curFolder = path.Substring(path.LastIndexOf(@"\"));
            if (curFolder == "\\Debug" || curFolder == "\\Release") {
                path = path + "\\..\\..\\";
            }
            else {
                path = path + "\\";
            }

            return path;
        }
    }
}
